using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using bugcollect.open;
using bugcollect.test.Properties;
using System.IO;

namespace bugcollect.test
{
    /// <summary>
    /// Unit tests for the bugcollect.open classes
    /// </summary>
    [TestClass]
    public class UTReport
    {
        static TestContext _testContext;
        private ReportingManager _reportingManager;

        public UTReport()
        {
            // Prior to running the test create an application credential.
            //  - Log in into your BugCollect.com account and go to the My Account page
            //  - In the 'Credentials authorized to submit reports to this account' add a new credential
            //  - Modify the bugcollect.test unit test to use the credentials you just created
            //
            Assert.IsFalse(
                String.IsNullOrEmpty(Settings.Default.ApplicationName) ||
                String.IsNullOrEmpty(Settings.Default.ApplicationPassword),
                @"Before running the test you must set up the application name and password used to submit reports. See http://bugcollect.com/faq#credential");

            _reportingManager = new ReportingManager(
                Settings.Default.ApplicationName,
                Settings.Default.ApplicationPassword);
            Assert.IsNotNull(_reportingManager);
        }

        #region Additional test attributes
        //
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) 
        {
            _testContext = testContext;
        }
        #endregion

        private void DescribeResult(ReportSubmissionResultArgs args)
        {
            if (null != args.Exception)
            {
                _testContext.WriteLine("Exception occured when submiting the report: {0}", args.Exception);
            }
            if (null != args.Result)
            {
                _testContext.WriteLine(
@"Report submission result: {0}
    Report ID: {1}
    Bucket ID: {2}
    Source ID: {3}", args.Result.Status, args.Result.ReportId, args.Result.BucketId, args.Result.SourceId);
                if (null != args.Result.Response)
                {
                    using (TextReader reader = new StreamReader(args.Result.Response))
                    {
                        _testContext.WriteLine("{0}", reader.ReadToEnd());
                    }
                }
            }
        }

        [TestMethod]
        public void SubmitException()
        {
            ReportSubmissionResultArgs args = null;
            try
            {
                throw new ApplicationException("Bugcollect.com Unit Test exception");
            }
            catch (Exception e)
            {
                _reportingManager.Submit(
                    null, e, null, delegate(object sender, ReportSubmissionResultArgs delegateArgs)
                    {
                        args = delegateArgs;
                    });
            }
            // Wait for the submisison to complete
            _reportingManager.WaitPendingDone.WaitOne();

            Assert.IsNotNull(args);
            DescribeResult(args);

            Assert.IsNull(args.Exception);
            Assert.IsNotNull(args.Result);
            Assert.AreEqual(EResultStatus.CREATED, args.Result.Status);
        }
    }
}
